package com.LeeCode;

import java.util.HashMap;

/**
 * 找出和为指定值的下标对
 */

public class Code1865 {
    public static void main(String[] args) {
        int[] nums1 = {1, 1, 2, 2, 2, 3};
        int[] nums2 = {1, 4, 5, 2, 5, 4};
        FindSumPairs findSumPairs = new Code1865().new FindSumPairs(nums1, nums2);
        System.out.println(findSumPairs.count(7));
        findSumPairs.add(3, 2);
        System.out.println(findSumPairs.count(8));
        System.out.println(findSumPairs.count(4));
        findSumPairs.add(0, 1);
        findSumPairs.add(1, 1);
        System.out.println(findSumPairs.count(7));
    }

    class FindSumPairs {
        private HashMap<Integer, Integer> map;
        private int[] nums1;
        private int[] nums2;

        public FindSumPairs(int[] nums1, int[] nums2) {
            map = new HashMap<>();
            this.nums1 = nums1;
            this.nums2 = nums2;
            for (int num : nums2) {
                map.merge(num, 1, Integer::sum);
            }
        }

        public void add(int index, int val) {
            map.merge(nums2[index], -1, Integer::sum);
            nums2[index] += val;
            map.merge(nums2[index], 1, Integer::sum);
        }

        public int count(int tot) {
            int count = 0;
            for (int num : nums1) {
                count += map.getOrDefault(tot - num, 0);
            }
            return count;
        }
    }
}
